package com.lenovo.smarttraffic.ui.fragment.stage4;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.charts.RadarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.data.RadarData;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.data.RadarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarInfo;
import com.lenovo.smarttraffic.bean.CarPeccancyBean;
import com.lenovo.smarttraffic.bean.PeccancyTypeBean;
import com.lenovo.smarttraffic.bean.UserBean;
import com.lenovo.smarttraffic.database.CarInfoBean;
import com.lenovo.smarttraffic.database.CarPeccancyDB;
import com.lenovo.smarttraffic.database.PeccancyTypeDB;
import com.lenovo.smarttraffic.ui.activity.stage7.DataAnalyzeActivity;
import com.lenovo.smarttraffic.ui.fragment.MyBaseFragment;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.Utils;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;

public class DataAnalyzeFragment extends MyBaseFragment {

    private Context context;
    private View view;

    private Gson gson = new Gson();

    private Handler handler = new Handler();

    private PieChart pieChart;
    private BarChart barChart;
    private HorizontalBarChart horizontalBarChart;
    private RadarChart radarChart;

    private int type = 0;

    private boolean animate_shown = false;

    public DataAnalyzeFragment setType(int type) {
        this.type = type;
        return this;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_data_analyze, container, false);
        TextView title = view.findViewById(R.id.title);

        String titleStr = "";
        switch (type) {
            case 0:
                titleStr = "平台上有违章车辆和没违章车辆的占比统计";
                pieChart = view.findViewById(R.id.pie_chart);
                pieChart.setVisibility(View.VISIBLE);
                break;
            case 1:
                titleStr = "有无“重复违章记录的车辆”的占比统计";
                pieChart = view.findViewById(R.id.pie_chart);
                pieChart.setVisibility(View.VISIBLE);
                break;
            case 2:
                titleStr = "违章车辆的违章次数占比分布图统计";
                horizontalBarChart = view.findViewById(R.id.h_bar_chart);
                horizontalBarChart.setVisibility(View.VISIBLE);
                break;
            case 3:
                titleStr = "年龄群体车辆违章的占比统计";
                barChart = view.findViewById(R.id.bar_chart);
                barChart.setVisibility(View.VISIBLE);
                break;
            case 4:
                titleStr = "平台上男性和女性有无车辆违章的占比统计";
                barChart = view.findViewById(R.id.bar_chart);
                barChart.setVisibility(View.VISIBLE);
                break;
            case 5:
                titleStr = "每日时段内车辆违章的占比统计";
                barChart = view.findViewById(R.id.bar_chart);
                barChart.setVisibility(View.VISIBLE);
                break;
            case 6:
                titleStr = "排名前十位的交通违法行为的占比统计";
                horizontalBarChart = view.findViewById(R.id.h_bar_chart);
                horizontalBarChart.setVisibility(View.VISIBLE);
                break;
            case 7:
                titleStr = "违章类型分析";
                radarChart = view.findViewById(R.id.radar_chart);
                radarChart.setVisibility(View.VISIBLE);
        }

        title.setText(titleStr);

        return view;
    }

    public void showChart() {
        if (!animate_shown) {
            animate_shown = true;
            switch (type) {
                case 0:
                case 1:
                    pieChart.animateY(1500, Easing.EasingOption.EaseInOutExpo);
                    break;
                case 2:
                case 6:
                    horizontalBarChart.animateY(1500, Easing.EasingOption.EaseInOutExpo);
                    break;
                case 7:
                    radarChart.animateY(1500, Easing.EasingOption.EaseInOutExpo);
                    break;
                default:
                    barChart.animateY(1500, Easing.EasingOption.EaseInOutExpo);
            }
        }
    }

    @Override
    public void onViewFinished() {
        switch (type) {
            case 0:
                initTab1();
                break;
            case 1:
                initTab2();
                break;
            case 2:
                initTab3();
                break;
            case 3:
                initTab4();
                break;
            case 4:
                initTab5();
                break;
            case 5:
                initTab6();
                break;
            case 6:
                initTab7();
                break;
            case 7:
                initTab8();
                break;
        }
    }


    private void initTab8() {
        //查询违章类型
        HttpUtils.post("get_peccancy_type", new HttpUtils().add("UserName", MainActivity.userName),
                response -> {
                    PeccancyTypeBean peccancyTypeBean = gson.fromJson(response.toString(), PeccancyTypeBean.class);
                    List<CarPeccancyBean.ROWSDETAILBean> beanList = DataAnalyzeActivity.beanList;
                    List<PeccancyTypeBean.ROWSDETAILBean> beanList2 = peccancyTypeBean.getROWS_DETAIL();

                    /*for (int i = 0; i < peccancyTypeBean.getROWS_DETAIL().size(); i++) {
                        PeccancyTypeBean.ROWSDETAILBean rowsdetailBean = peccancyTypeBean.getROWS_DETAIL().get(i);
                        new PeccancyTypeDB(rowsdetailBean.getPcode(), rowsdetailBean.getPmoney(),
                                rowsdetailBean.getPscore(), rowsdetailBean.getPremarks()).save();
                    }

                    for (int i = 0; i < beanList.size(); i++) {
                        CarPeccancyBean.ROWSDETAILBean rowsdetailBean = beanList.get(i);
                        new CarPeccancyDB(rowsdetailBean.getId(), rowsdetailBean.getCarnumber(),
                                rowsdetailBean.getPcode(), rowsdetailBean.getPdatetime(), rowsdetailBean.getPaddr()).save();
                    }*/

                    List<String> why10 = new ArrayList<>();
                    List<Integer> why10Count = new ArrayList<>();
                    Cursor bySQL = LitePal.findBySQL("SELECT peccancytypedb.premarks , COUNT(*)  FROM peccancytypedb inner JOIN  carpeccancydb on carpeccancydb.pcode = peccancytypedb.pcode GROUP BY peccancytypedb.premarks ORDER BY COUNT(*) desc");
                    bySQL.moveToFirst();

                    for (int i = 0; i < 10; i++) {
                        bySQL.move(i);
                        why10.add(bySQL.getString(0));
                        why10Count.add(bySQL.getInt(1));
                    }
                    bySQL.close();

                    ArrayList<RadarEntry> radarEntries = new ArrayList<>();
                    for (Integer count : why10Count) {
                        radarEntries.add(new RadarEntry(count));
                    }

                    ArrayList<Integer> integers = new ArrayList<>();
                    integers.add(0xffEE82EE);
                    integers.add(0xff9932CC);
                    integers.add(0xffB0C4DE);
                    integers.add(0xff87CEEB);
                    integers.add(0xff66cccc);
                    integers.add(0xff3CB371);
                    integers.add(0xffD2B48C);
                    integers.add(0xffFF7F50);
                    integers.add(0xffA52A2A);
                    integers.add(0xff808080);

                    RadarDataSet radarDataSet = new RadarDataSet(radarEntries, "");
                    radarDataSet.setDrawFilled(true);
                    radarDataSet.setColors(integers);
                    radarDataSet.setValueTextColors(integers);
                    radarDataSet.setValueTextSize(8);

                    radarDataSet.setFillColor(0x9066cccc);
                    RadarData radarData = new RadarData(radarDataSet);
                    radarChart.setTouchEnabled(false);
                    radarChart.setData(radarData);
                });
    }


    private void initTab7() {
        //查询违章类型
        HttpUtils.post(context, "get_peccancy_type", new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> {
                    PeccancyTypeBean peccancyTypeBean = gson.fromJson(response.toString(), PeccancyTypeBean.class);
                    List<CarPeccancyBean.ROWSDETAILBean> beanList = DataAnalyzeActivity.beanList;
                    List<PeccancyTypeBean.ROWSDETAILBean> beanList2 = peccancyTypeBean.getROWS_DETAIL();

                    /*for (int i = 0; i < peccancyTypeBean.getROWS_DETAIL().size(); i++) {
                        PeccancyTypeBean.ROWSDETAILBean rowsdetailBean = peccancyTypeBean.getROWS_DETAIL().get(i);
                        new PeccancyTypeDB(rowsdetailBean.getPcode(), rowsdetailBean.getPmoney(),
                                rowsdetailBean.getPscore(), rowsdetailBean.getPremarks()).save();
                    }

                    for (int i = 0; i < beanList.size(); i++) {
                        CarPeccancyBean.ROWSDETAILBean rowsdetailBean = beanList.get(i);
                        new CarPeccancyDB(rowsdetailBean.getId(), rowsdetailBean.getCarnumber(),
                                rowsdetailBean.getPcode(), rowsdetailBean.getPdatetime(), rowsdetailBean.getPaddr()).save();
                    }*/

                    List<String> why10 = new ArrayList<>();
                    List<Integer> why10Count = new ArrayList<>();
                    Cursor bySQL = LitePal.findBySQL("SELECT peccancytypedb.premarks , COUNT(*)  FROM peccancytypedb inner JOIN  carpeccancydb on carpeccancydb.pcode = peccancytypedb.pcode GROUP BY peccancytypedb.premarks ORDER BY COUNT(*) desc");
                    bySQL.moveToFirst();

                    for (int i = 0; i < 10; i++) {
                        bySQL.move(i);
                        why10.add(bySQL.getString(0));
                        why10Count.add(bySQL.getInt(1));
                    }
                    bySQL.close();


                    //反转
                    Collections.reverse(why10Count);
                    Collections.reverse(why10);
                    int all = 0;
                    for (int i = 0; i < why10Count.size(); i++) {
                        all += why10Count.get(i);
                    }
                    //显示图表
                    List<BarEntry> barEntries = new ArrayList<>();
                    for (int i = 0; i < why10Count.size(); i++) {
                        barEntries.add(new BarEntry(i, ((float) why10Count.get(i) / all) * 100));
                    }

                    BarDataSet barDataSet = new BarDataSet(barEntries, "");
                    BarData barData = new BarData(barDataSet);
                    barData.setValueTextSize(16);
                    barDataSet.setColors(0xff9932CC, 0xffEE82EE, 0xffB0C4DE, 0xff87CEEB, 0xff66cccc, 0xff3CB371
                            , 0xffD2B48C, 0xffFF7F50, 0xffA52A2A, 0xff808080);
                    barDataSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> String.format(Locale.ENGLISH, "%.2f", value) + "%");

                    XAxis xAxis = horizontalBarChart.getXAxis();
                    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
                    xAxis.setTextSize(14);
                    xAxis.setTypeface(Typeface.MONOSPACE);
                    xAxis.setLabelCount(10);
                    xAxis.setDrawGridLines(false);
                    xAxis.setValueFormatter((value, axis) -> why10.get((int) value));

                    YAxis yAxis = horizontalBarChart.getAxisRight();
                    yAxis.setTextSize(15);
                    yAxis.setValueFormatter((value, axis) -> (int) value + ".00%");

                    horizontalBarChart.getAxisLeft().setEnabled(false);
                    horizontalBarChart.setTouchEnabled(false);
                    horizontalBarChart.setData(barData);
                });
    }

    private void initTab6() {
        List<CarPeccancyBean.ROWSDETAILBean> beanList = DataAnalyzeActivity.beanList;

        int time0 = 0, time2 = 0, time4 = 0, time6 = 0, time8 = 0, time10 = 0, time12 = 0, time14 = 0, time16 = 0, time18 = 0, time20 = 0, time22 = 0;
        for (int i = 0; i < beanList.size(); i++) {
            switch (beanList.get(i).getPdatetime().substring(11, 13)) {
                case "00":
                    time0++;
                    break;
                case "02":
                    time2++;
                    break;
                case "04":
                    time4++;
                    break;
                case "06":
                    time6++;
                    break;
                case "08":
                    time8++;
                    break;
                case "10":
                    time10++;
                    break;
                case "12":
                    time12++;
                    break;
                case "14":
                    time14++;
                    break;
                case "16":
                    time16++;
                    break;
                case "18":
                    time18++;
                    break;
                case "20":
                    time20++;
                    break;
                case "22":
                    time22++;
                    break;
            }
        }
        int[] timeAll = {time0, time2, time4, time6, time8, time10, time12, time14, time16, time18, time20, time22};


        float all = 0;
        for (int i : timeAll) {
            all += i;
        }
        ArrayList<BarEntry> barEntries = new ArrayList<>();

        for (int i = 0; i < timeAll.length; i++) {
            barEntries.add(new BarEntry(i, (timeAll[i] / all) * 100));
        }

        BarDataSet barDataSet = new BarDataSet(barEntries, "");
        barDataSet.setColors(0xffFFC0CB, 0xff7B68EE, 0xff4169E1, 0xff708090, 0xff008B8B, 0xff66cccc, 0xff90EE90
                , 0xffFFA500, 0xffD2B48C, 0xffF08080, 0xffA52A2A, 0xff00BFFF);
        barDataSet.setValueTextSize(15);
        barDataSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> String.format(Locale.ENGLISH, "%.1f", value) + "%");
        BarData barData = new BarData(barDataSet);
        XAxis xAxis = barChart.getXAxis();
        xAxis.setTextSize(14);
        xAxis.setLabelRotationAngle(-45);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(timeAll.length);
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                switch ((int) value) {
                    case 0:
                        return "0:00-2:00";
                    case 1:
                        return "2:00-4:00";
                    case 2:
                        return "4:00-6:00";
                    case 3:
                        return "6:00-8:00";
                    case 4:
                        return "8:00-10:00";
                    case 5:
                        return "10:00-12:00";
                    case 6:
                        return "12:00-14:00";
                    case 7:
                        return "14:00-16:00";
                    case 8:
                        return "16:00-18:00";
                    case 9:
                        return "18:00-20:00";
                    case 10:
                        return "20:00-22:00";
                    case 11:
                        return "22:00-24:00";
                }
                return "error";
            }
        });

        Description description = new Description();
        description.setEnabled(false);
        barChart.getAxisLeft().setTextSize(15);
        barChart.getAxisRight().setEnabled(false);
        barChart.setDescription(description);
        barChart.setTouchEnabled(false);
        barChart.setData(barData);
    }

    private void initTab5() {
        HttpUtils.post(context, "get_car_info", new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> HttpUtils.post(context, "get_all_user_info", new HttpUtils().setParam("UserName", MainActivity.userName),
                        response2 -> {
                            CarInfo carInfo = gson.fromJson(response.toString(), CarInfo.class);
                            UserBean userBean = gson.fromJson(response2.toString(), UserBean.class);
                            List<CarPeccancyBean.ROWSDETAILBean> beanList = DataAnalyzeActivity.beanList;
                            List<CarInfo.ROWSDETAILBean> car_list = carInfo.getROWS_DETAIL();
                            List<UserBean.ROWS_DETAIL> user_list = userBean.getROWS_DETAIL();

                            int[] have = {0, 0};//male,female
                            for (int i = 0; i < beanList.size(); i++) {
                                for (int j = 0; j < car_list.size(); j++) {
                                    if (beanList.get(i).getCarnumber().equals(car_list.get(j).getCarnumber())) {
                                        if (Utils.parseInt(car_list.get(j).getPcardid().substring(9, 10)) % 2 == 0) {
                                            have[0]++;
                                        } else have[1]++;
                                    }
                                }
                            }

                            int[] not = {0, 0};//male,female
                            for (int i = 0; i < user_list.size(); i++) {
                                if (user_list.get(i).getPsex().equals("男")) {
                                    have[0]++;
                                } else {
                                    have[1]++;
                                }
                            }

                            //Delete
                            not[0] -= have[0];
                            not[1] -= have[1];

                            float AllHave = have[0] + have[1];
                            float AllNot = not[0] + not[1];

                            List<BarEntry> barHaveEntries = new ArrayList<>();
                            List<BarEntry> barNotHaveEntries = new ArrayList<>();

                            barHaveEntries.add(new BarEntry(0, (have[0] / AllHave) * 100));
                            barHaveEntries.add(new BarEntry(1, (have[1] / AllHave) * 100));

                            barNotHaveEntries.add(new BarEntry(0, (not[0] / AllNot) * 100 + ((have[0] / AllHave) * 100)));
                            barNotHaveEntries.add(new BarEntry(1, (not[1] / AllNot) * 100 + ((have[1] / AllHave) * 100)));

                            BarDataSet barDataSet = new BarDataSet(barHaveEntries, "有违法");
                            barDataSet.setValueTextSize(15);
                            BarDataSet barNotDataSet = new BarDataSet(barNotHaveEntries, "无违法");
                            barNotDataSet.setValueTextSize(15);
                            //barNotDataSet.setDrawValues(false);

                            barDataSet.setColor(0xff66cccc);
                            barDataSet.setValueTextColor(0xff66cccc);
                            barNotDataSet.setColor(0xffFF4500);
                            barNotDataSet.setValueTextColor(0xffFF4500);

                            List<IBarDataSet> barDataSets = new ArrayList<>();
                            barDataSets.add(barNotDataSet);
                            barDataSets.add(barDataSet);

                            barDataSet.setValueFormatter(new IValueFormatter() {
                                @Override
                                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                                    return String.format(Locale.ENGLISH, "%.1f", value) + "%";
                                }
                            });
                            barNotDataSet.setValueFormatter(new IValueFormatter() {
                                @Override
                                public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                                    return String.format(Locale.ENGLISH, "%.1f", value / 2) + "%";
                                }
                            });

                            BarData barData = new BarData(barDataSets);
                            barData.setBarWidth(0.4f);

                            XAxis xAxis = barChart.getXAxis();
                            xAxis.setTextSize(16);
                            xAxis.setLabelCount(2);
                            xAxis.setDrawGridLines(false);
                            xAxis.setValueFormatter(new IAxisValueFormatter() {
                                @Override
                                public String getFormattedValue(float value, AxisBase axis) {
                                    return (int) value == 0 ? "男" : "女";
                                }
                            });
                            xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

                            YAxis yAxis = barChart.getAxisLeft();
                            yAxis.setTextSize(16);

                            barChart.getAxisRight().setEnabled(false);
                            barChart.setTouchEnabled(false);
                            barChart.setData(barData);
                        }));
    }

    private void initTab4() {
        //查询违章车辆
        HttpUtils.post(context, "get_car_info", new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> {
                    List<CarPeccancyBean.ROWSDETAILBean> beanList = DataAnalyzeActivity.beanList;
                    CarInfo carInfo = gson.fromJson(response.toString(), CarInfo.class);
                    List<CarInfo.ROWSDETAILBean> beanList1 = carInfo.getROWS_DETAIL();

                    int have90 = 0, have80 = 0, have70 = 0, have60 = 0, have50 = 0;
                    for (int i = 0; i < beanList.size(); i++) {
                        for (int j = 0; j < beanList1.size(); j++) {
                            //Check car number
                            if (beanList.get(i).getCarnumber().equals(beanList1.get(j).getCarnumber())) {
                                switch (beanList1.get(j).getPcardid().substring(8, 9)) {
                                    case "9":
                                        have90++;
                                        break;
                                    case "8":
                                        have80++;
                                        break;
                                    case "7":
                                        have70++;
                                        break;
                                    case "6":
                                        have60++;
                                        break;
                                    case "5":
                                        have50++;
                                        break;
                                }
                            }
                        }
                    }
                    int[] haves = {have90, have80, have70, have60, have50};

                    int not90 = 0, not80 = 0, not70 = 0, not60 = 0, not50 = 0;
                    for (int i = 0; i < beanList1.size(); i++) {
                        switch (beanList1.get(i).getPcardid().substring(8, 9)) {
                            case "9":
                                not90++;
                                break;
                            case "8":
                                not80++;
                                break;
                            case "7":
                                not70++;
                                break;
                            case "6":
                                not60++;
                                break;
                            case "5":
                                not50++;
                                break;
                        }
                    }
                    int[] nots = {not90 - have90, not80 - have80, not70 - have70, not60 - have60, not50 - have50};

                    float AllHave = haves[0] + haves[1] + haves[2] + haves[3] + haves[4];
                    float AllNot = nots[0] + nots[1] + nots[2] + nots[3] + nots[4];

                    List<BarEntry> barHaveEntries = new ArrayList<>();
                    List<BarEntry> barNotHaveEntries = new ArrayList<>();

                    barHaveEntries.add(new BarEntry(0, (haves[0] / AllHave) * 100));
                    barHaveEntries.add(new BarEntry(1, (haves[1] / AllHave) * 100));
                    barHaveEntries.add(new BarEntry(2, (haves[2] / AllHave) * 100));
                    barHaveEntries.add(new BarEntry(3, (haves[3] / AllHave) * 100));
                    barHaveEntries.add(new BarEntry(4, (haves[4] / AllHave) * 100));

                    barNotHaveEntries.add(new BarEntry(0, (nots[0] / AllNot) * 100 + ((haves[0] / AllHave) * 100)));
                    barNotHaveEntries.add(new BarEntry(1, (nots[1] / AllNot) * 100 + ((haves[1] / AllHave) * 100)));
                    barNotHaveEntries.add(new BarEntry(2, (nots[2] / AllNot) * 100 + ((haves[2] / AllHave) * 100)));
                    barNotHaveEntries.add(new BarEntry(3, (nots[3] / AllNot) * 100 + ((haves[3] / AllHave) * 100)));
                    barNotHaveEntries.add(new BarEntry(4, (nots[4] / AllNot) * 100 + ((haves[4] / AllHave) * 100)));

                    BarDataSet barDataSet = new BarDataSet(barHaveEntries, "有违章");
                    barDataSet.setValueTextSize(15);
                    BarDataSet barNotDataSet = new BarDataSet(barNotHaveEntries, "无违章");
                    barNotDataSet.setValueTextSize(15);
                    //barNotDataSet.setDrawValues(false);

                    barDataSet.setColor(0xff66cccc);
                    barDataSet.setValueTextColor(0xff66cccc);
                    barNotDataSet.setColor(0xffFF4500);
                    barNotDataSet.setValueTextColor(0xffFF4500);

                    List<IBarDataSet> barDataSets = new ArrayList<>();
                    barDataSets.add(barNotDataSet);
                    barDataSets.add(barDataSet);

                    barDataSet.setValueFormatter(new IValueFormatter() {
                        @Override
                        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                            return String.format(Locale.ENGLISH, "%.1f", value) + "%";
                        }
                    });
                    barNotDataSet.setValueFormatter(new IValueFormatter() {
                        @Override
                        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
                            return String.format(Locale.ENGLISH, "%.1f", value / 2) + "%";
                        }
                    });

                    BarData barData = new BarData(barDataSets);
                    barData.setBarWidth(0.4f);

                    XAxis xAxis = barChart.getXAxis();
                    xAxis.setTextSize(16);
                    xAxis.setLabelCount(5);
                    xAxis.setDrawGridLines(false);
                    xAxis.setValueFormatter(new IAxisValueFormatter() {
                        @Override
                        public String getFormattedValue(float value, AxisBase axis) {
                            return (9 - (int) value) + "0后";
                        }
                    });
                    xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);

                    YAxis yAxis = barChart.getAxisLeft();
                    yAxis.setTextSize(16);

                    barChart.getAxisRight().setEnabled(false);
                    barChart.setTouchEnabled(false);
                    barChart.setData(barData);
                });
    }

    private void initTab3() {
        //查询违章车辆
        List<CarPeccancyBean.ROWSDETAILBean> beanList = DataAnalyzeActivity.beanList;

        //获取重复数量
        float one = 0, two = 0, three = 0;
        String tempStr;
        int tempInt = 0;
        for (int i = 0; i < beanList.size(); i++, tempInt = 0) {
            tempStr = beanList.get(i).getCarnumber();
            for (int j = i + 1; j < beanList.size(); j++) {
                if (tempStr.equals(beanList.get(j).getCarnumber())) {
                    tempInt++;
                }
            }
            if (tempInt > 5) {
                three++;
            } else if (tempInt >= 3) {
                two++;
            } else {
                one++;
            }
        }

        float allCar = beanList.size();
        List<BarEntry> barEntries = new ArrayList<>();
        float one2 = (float) ((int) ((one / allCar) * 10000)) / 100;
        float three5 = (float) ((int) ((two / allCar) * 10000)) / 100;
        float fivePlus = (float) ((int) ((three / allCar) * 10000)) / 100;
        barEntries.add(new BarEntry(0, one2));
        barEntries.add(new BarEntry(1, three5));
        barEntries.add(new BarEntry(2, fivePlus));

        BarDataSet barDataSet = new BarDataSet(barEntries, "");
        BarData barData = new BarData(barDataSet);
        barData.setValueTextSize(16);
        barDataSet.setColors(0xff66cccc, 0xff009ACD, 0xffCD5C5C);
        barDataSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> value + "%");

        XAxis xAxis = horizontalBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(14);
        xAxis.setTypeface(Typeface.MONOSPACE);
        xAxis.setLabelCount(3);
        xAxis.setDrawGridLines(false);
        xAxis.setValueFormatter((value, axis) -> {
            switch ((int) value) {
                case 0:
                    return "1-2条违章";
                case 1:
                    return "3-5条违章";
                case 2:
                    return "5条以上违章";
                default:
                    return "error";
            }
        });

        YAxis yAxis = horizontalBarChart.getAxisRight();
        yAxis.setTextSize(15);
        yAxis.setValueFormatter((value, axis) -> (int) value + ".00%");

        horizontalBarChart.getAxisLeft().setEnabled(false);
        horizontalBarChart.setHighlightPerTapEnabled(false);
        horizontalBarChart.setHighlightPerDragEnabled(false);
        horizontalBarChart.setPinchZoom(true);
        horizontalBarChart.setData(barData);
    }

    private void initTab2() {
        //查询违章车辆
        List<CarPeccancyBean.ROWSDETAILBean> beanList = DataAnalyzeActivity.beanList;
        float reCount = 0;

        //获取重复数量
        String temp;
        for (int i = 0; i < beanList.size(); i++) {
            temp = beanList.get(i).getCarnumber();
            for (int j = i + 1; j < beanList.size(); j++) {
                if (temp.equals(beanList.get(j).getCarnumber())) {
                    reCount++;
                    break;
                }
            }
        }

        float allCar = beanList.size();
        float single = allCar - reCount;

        //保留一位小数
        float x = (float) ((int) ((reCount / allCar) * 1000)) / 10;
        float y = (float) ((int) ((single / allCar) * 1000)) / 10;

        List<PieEntry> pieEntries = new ArrayList<>();
        pieEntries.add(new PieEntry(x, "有重复违章"));
        pieEntries.add(new PieEntry(y, "无重复违章"));

        PieDataSet pieDataSet = new PieDataSet(pieEntries, "");
        pieDataSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> value + "%");
        pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        pieDataSet.setValueLinePart1OffsetPercentage(80);
        pieDataSet.setColors(0xFFCD5C5C, 0xFF1E90FF);
        pieDataSet.setValueTextSize(18);

        PieData pieData = new PieData(pieDataSet);
        pieChart.setDrawHoleEnabled(false);
        pieChart.setData(pieData);
    }

    private void initTab1() {
        //查询违章车辆
        List<CarPeccancyBean.ROWSDETAILBean> beanList = DataAnalyzeActivity.beanList;
        //去重
        HashSet<String> set = new HashSet<>();
        for (int i = 0; i < beanList.size(); i++) {
            set.add(beanList.get(i).getCarnumber());
        }

        float reccancyCount = set.size();
        float notReccancy = LitePal.count(CarInfoBean.class) - reccancyCount;
        float allcar = reccancyCount + notReccancy;

        //保留两位小数
        float x = (float) ((int) ((reccancyCount / allcar) * 10000)) / 100;
        float y = (float) ((int) ((notReccancy / allcar) * 10000)) / 100;

        List<PieEntry> pieEntries = new ArrayList<>();
        pieEntries.add(new PieEntry(x, "有违章"));
        pieEntries.add(new PieEntry(y, "无违章"));

        PieDataSet pieDataSet = new PieDataSet(pieEntries, "");
        pieDataSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> value + "%");
        pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        pieDataSet.setValueLinePart1OffsetPercentage(80);
        pieDataSet.setColors(0xFF1E90FF, 0xFFCD5C5C);
        pieDataSet.setValueTextSize(18);

        PieData pieData = new PieData(pieDataSet);
        pieChart.setDrawHoleEnabled(false);

        handler.postDelayed(() -> {
            animate_shown = true;
            pieChart.setData(pieData);
            pieChart.animateY(1500, Easing.EasingOption.EaseInOutExpo);
        }, 400);
    }

}